package com.ywh.hilttest

import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import com.ywh.hilttest.test1.FragmentLocalObject
import com.ywh.hilttest.test2.FragmentSdkObject
import com.ywh.hilttest.test3.FragmentObjectName
import com.ywh.hilttest.test4.FragmentObjectBind
import com.ywh.hilttest.test5.FragmentObjectGlobalSingleton1
import com.ywh.hilttest.test6.FragmentObjectActivitySingleton1
import com.ywh.hilttest.test7.FragmentContextObject
import com.ywh.hilttest.test8.FragmentViewModel
import com.ywh.hilttest.ui.theme.HiltTestTheme

class MainActivity : ComponentActivity() {

    private val items =
        arrayListOf(
            "创建本地对象",
            "创建第三方SDK里的对象",
            "创建的对象有多个构造函数",
            "创建的对象有多个子类",
            "全局单例测试",
            "Activity单例测试",
            "Context",
            "ViewModel"
        )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            HiltTestTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    LazyColumn {
                        itemsIndexed(items = items) { index, item ->
                            Text(
                                text = item, modifier = Modifier
                                    .fillMaxSize()
                                    .height(Dp(40f))
                                    .clickable {
                                        jump(index, item)
                                    }, style = TextStyle(textAlign = TextAlign.Center)
                            )
                        }
                    }
                }
            }
        }
    }

    private fun jump(index: Int, item: String) {
        val targetFragment = when (index) {
            0 -> FragmentLocalObject::class
            1 -> FragmentSdkObject::class
            2 -> FragmentObjectName::class
            3 -> FragmentObjectBind::class
            4 -> FragmentObjectGlobalSingleton1::class
            5 -> FragmentObjectActivitySingleton1::class
            6 -> FragmentContextObject::class
            7 -> FragmentViewModel::class
            else -> FragmentLocalObject::class
        }
        startActivity(
            Intent(
                this@MainActivity,
                ContainerActivity::class.java
            ).putExtra("containerClassName", targetFragment.java.name)
        )
    }
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        modifier = modifier
    )
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    HiltTestTheme {
        Greeting("Android")
    }
}